﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using mitasoft.AdsShedule.Properties;

namespace mitasoft.AdsShedule.Common
{
    public static class AppCommon
    {
        private static string Date_Format = "MMM-yyyy";

        private static DateTime selectedMonth;
        public static DateTime SelectedMonth
        {
            get
            {
                return selectedMonth;
            }
            set
            {
                selectedMonth = value;
            }
        }

        public static string SelectedMonthFormated
        {
            get
            {
                if (SelectedMonth != null)
                {
                    return SelectedMonth.ToString(Date_Format);
                }

                return DateTime.Now.ToString(Date_Format);
            }
        }
        public static List<DateTime> GetDaysInMonth()
        {
            List<DateTime> dates = new List<DateTime>();
            var days = DateTime.DaysInMonth(Common.AppCommon.SelectedMonth.Year, Common.AppCommon.SelectedMonth.Month);

            for (int d = 1; d <= days; d++)
            {
                dates.Add(new DateTime(Common.AppCommon.SelectedMonth.Year,
                    Common.AppCommon.SelectedMonth.Month, d));
            }

            return dates;
        }

        public static string GetFileName(int year, int month)
        {
            return string.Format("{0}-{1}.xml", year, month);
        }
        public static string GetDatedPath(int year, int month)
        {
            string fileName = GetFileName(year, month);
            string fileFolder = Settings.Default.DataFilesFolder;

            string startPath = Application.StartupPath;
            string firstPart = Path.Combine(startPath, fileFolder);
            string secondPart = Path.Combine(firstPart, fileName);

            return secondPart;
        }
        public static string GetTitledPath(string fileName)
        {
            string fileFolder = Settings.Default.DataFilesFolder;
            string startPath = Application.StartupPath;
            string firstPart = Path.Combine(startPath, fileFolder);
            string secondPart = Path.Combine(firstPart, fileName);

            return secondPart;
        }
        public static string GetReportsPath(string fileName)
        {
            string fileFolder = "Reports";
            string startPath = Application.StartupPath;
            string firstPart = Path.Combine(startPath, fileFolder);
            string secondPart = Path.Combine(firstPart, fileName);

            return secondPart;
        }

        public static bool ExistFile(int year, int month)
        {
            string filePath = GetDatedPath(year, month);
            return File.Exists(filePath);
        }
        public static void EnsureDataFilesFolder()
        {
            string fileFolder = Settings.Default.DataFilesFolder;
            string startPath = Application.StartupPath;
            string firstPart = Path.Combine(startPath, fileFolder);

            Directory.CreateDirectory(firstPart);
        }
    }
}
